import os
import json

cur_dir = os.path.dirname(os.path.abspath(__file__))

# 进行验证的 2 个 json 文件
json_file_original = os.path.join(cur_dir, 'annotation_result_raw.json')
json_file_processed = os.path.join(cur_dir, 're_dict.json')


# 验证 ent 数量一致
def validate_ent():
    # 跟据 json_file_original 文件中的元素特点进行筛选，如果一个元素有 'value' 键，并且其值部分有 'labels' 键，则计数器加一，最终统计满足条件的总数
    with open(json_file_original, 'r') as f:
        data_origianl = json.load(f)
        count_origianl = 0
        for item in data_origianl['result']:
            if 'value' in item and 'labels' in item['value']:
                count_origianl += 1
        print(f"Original JSON file contains {count_origianl} entities.")

    # 统计 json_file_processed 文件中 'ent' 键对应的值（列表）中的元素个数
    with open(json_file_processed, 'r') as f:
        data_processed = json.load(f)
        count_processed = len(data_processed['ent'])
        print(f"Processed JSON file contains {count_processed} entities.")

    # 比较两个计数器的值是否相等
    if count_origianl == count_processed:
        print("Ent validation passed.")
    else:
        print("Ent validation failed.")

# 验证 rel 数量一致
def validate_rel():
    # 跟据 json_file_original 文件中的元素特点进行筛选，如果一个元素有 "type": "relation" 键值对，则计数器加一，最终统计满足条件的总数
    with open(json_file_original, 'r') as f:
        data_origianl = json.load(f)
        count_origianl = 0
        for item in data_origianl['result']:
            if 'type' in item and item['type'] == 'relation':
                count_origianl += 1

        # rel 总数要加上动作数量 - 1
        action_count = 0
        with open(json_file_processed, 'r') as f2:
            data_processed = json.load(f2)
            action_count = len(data_processed['action'])
        count_origianl += action_count - 1
        print(f"Original JSON file contains {count_origianl} relations.")

    # 统计 json_file_processed 文件中 'rel' 键对应的值（列表）中的元素个数
    with open(json_file_processed, 'r') as f:
        data_processed = json.load(f)
        count_processed = len(data_processed['rel'])
        print(f"Processed JSON file contains {count_processed} relations.")

    # 比较两个计数器的值是否相等
    if count_origianl == count_processed:
        print("Rel validation passed.")
    else:
        print("Rel validation failed.")


# 各种标签列表
action_types = ["single-abstract","multi-abstract", "single-specific-situ-without-a-target", "single-specific-situ-with-a-target", "single-specific-move-without-a-target", "single-specific-move-with-a-target", "single-specific-appear", "single-specific-situ-leave", "single-specific-move-leave", "multi-specific-situ-no-touch", "multi-specific-situ-touch", "multi-specific-move-no-touch", "multi-specific-move-touch"]
other_action_types = ["reference", "emotion", "prepare", "speak-content", "thought-content", "status"]

# 验证 act 数量一致
def validate_act():
    # 跟据 json_file_original 文件中的元素特点进行筛选，如果一个元素有 'value' 键，并且其值部分有 'labels' 键，并且，'labels' 键的所有值都包含在 action_types 和 other_action_types 两个列表的元素的并集中，则计数器加一，最终统计满足条件的总数
    with open(json_file_original, 'r') as f:
        data_origianl = json.load(f)
        count_origianl = 0
        for item in data_origianl['result']:
            if 'value' in item and 'labels' in item['value']:
                labels = item['value']['labels']
                if all(label in action_types + other_action_types for label in labels):
                    count_origianl += 1
        print(f"Original JSON file contains {count_origianl} actions.")
    
    # 统计 json_file_processed 文件中 'action' 键对应的值（列表）中的元素个数
    with open(json_file_processed, 'r') as f:
        data_processed = json.load(f)
        count_processed = len(data_processed['action'])
        print(f"Processed JSON file contains {count_processed} actions.")

    # 比较两个计数器的值是否相等
    if count_origianl == count_processed:
        print("Act validation passed.")
    else:
        print("Act validation failed.")


if __name__ == "__main__":
    validate_ent()
    validate_rel()
    validate_act()
